package com.hxgz.steel.controller.common;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.AskInfo;
import com.hxgz.steel.util.OSSClientUtil;
import com.hxgz.steel.util.PropertiesUtil;
import com.hxgz.steel.util.UploadPic;
import com.hxgz.steel.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 上传图片
 */
@RestController
@RequestMapping("Common")
@Api(tags = "CommonController",description ="公共接口" )
public class CommonController {

    @Autowired
    public OSSClientUtil ossutil;

    @PostMapping(value = "getPicAddress.do",headers = "content-type=multipart/form-data")
    @ApiOperation("文件上传")
    public ServerResponse<String> getPicAddress(@RequestParam(value = "file",required = true) MultipartFile multipartFile, HttpServletRequest request){
       String imgurl = "";
        try {
            String name = ossutil.uploadImg2Oss(multipartFile);
            imgurl = ossutil.getImgUrl(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.createBySuccess(imgurl);
    }

    @PostMapping(value = "getPicAddressList.do",headers = "content-type=multipart/form-data")
    @ApiOperation("文件上传")
    public Map<String,Object> getPicAddressList(@RequestParam(value = "file",required = true) MultipartFile multipartFile, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        UploadPic pic = new UploadPic();
        String filePath = pic.upload(multipartFile);
        File file = new File(filePath);
        String fileName = file.getName();
        //文件请求路径
        String pealPath = PropertiesUtil.getProperty("file.server.peal") + fileName;
        List<String> imgList= new ArrayList<>();
        imgList.add(pealPath);
        map.put("errno",0);
        map.put("data",imgList);
        return map;
    }

    @RequestMapping(value = "exportExcel.do",method = RequestMethod.POST)
    @ApiOperation("导出Excel")
    public ServerResponse exportExcel(HttpServletResponse response){
        List<AskInfo> list =new ArrayList<>();
        List<Map<String, String>> dataList = Lists.newArrayList();
        for (AskInfo vo : list) {
            Map<String, String> dataMap = Maps.newHashMap();
            dataMap.put("品名",vo.getCommodity());
            dataMap.put("材质",vo.getTexture());
            dataMap.put("规格",vo.getSpecification());
            dataMap.put("数量 / 单位",vo.getQuantity());
            dataMap.put("地区",vo.getArea());
            dataMap.put("报价","0");
            dataList.add(dataMap);
        }
        String excelName = "求购文件";
        String[] columnName = {"品名","材质","规格","数量 / 单位","地区","报价"};
        ExcelUtil util = ExcelUtil.getInstance();
        Workbook workbook = util.dataToExcel(dataList, excelName, columnName);
        System.out.println("当前类文件的编码格式"+ Charset.defaultCharset().name());
        System.out.println("Workbook的编码   ");
        OutputStream output = null;
        try {
            output = response.getOutputStream();
            System.out.println("response的乱码问题"+response.getCharacterEncoding());
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
//            response.setHeader("Content-Disposition", "attachment;filename="+
//                    new String(("采购明细列表" + ".xls").getBytes(), "iso-8859-1"));
//            response.setHeader("Content-Disposition", "attachment;filename="+
//                    new String(("采购明细列表").getBytes("gb2312"), "ISO-8859-1")+".xls");
            response.setHeader("Content-Disposition", "attachment;filename="+
                    new String(java.net.URLEncoder.encode(excelName,"UTF-8")+".xls")
                    );

            workbook.write(output);
            output.flush();
            output.close();
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }
}
